package com.wanpinghui.supplier.user.company;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.dialog.LoadingDialog;
import com.wanpinghui.wphlibrary.model.business.BusinessService;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.CompanyBean;
import com.wanpinghui.wphlibrary.photovideo.bean.BaseUrlBean;
import com.wanpinghui.wphlibrary.photovideo.bean.UploadFileBean;
import com.wanpinghui.wphlibrary.photovideo.view.ClipActivity;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.PhotoPickManager;
import retrofit.callback.YRequestCallback;

/**
 * Created by tg596 on 2018/1/23.
 */
@ContentView(R.layout.layout_activity_company_baseinfo)
public class CompanyBaseInfoActivity extends BaseActivity {

    private static final int REQUEST_SELECT_PHOTO = 0x01;
    private static final int CLIP_PHOTO = 0x12;

    private ArrayList<UploadFileBean> userList;
    private ImageView mMViewBacktrack;

    private TextView tv_warning_text;
    /**
     * 上传公司LOGO图片
     */
    private FrameLayout mFlUploadImg;
    private TextView mTvUploadImg;
//    private ImageView mIvCompanyLogo;
    /**
     * 河南晶佳科技有限公司
     */
    private TextView mTvCompanyName;
    /**
     * 请输入职称
     */
    private YunShlEditText mEdtCompanyPos;
    /**
     * 请输入所在区域
     */
    private YunShlEditText mEdtCompanyAddr;
    /**
     * 输入内容（600字）
     */
    private YunShlEditText mEdtCompanyInfo;
    /**
     * 下一步
     */
    private ThrottleButton mBtnNext;

    private CompanyBean bean;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        mMViewBacktrack = (ImageView) findViewById(R.id.mViewBacktrack);
        mFlUploadImg = (FrameLayout) findViewById(R.id.fl_upload_img);
        mTvUploadImg = (TextView) findViewById(R.id.tv_upload_img);
    //    mIvCompanyLogo = (ImageView)findViewById(R.id.iv_company_logo);
        mTvCompanyName = (TextView) findViewById(R.id.tv_company_name);
        mEdtCompanyPos = (YunShlEditText) findViewById(R.id.edt_company_pos);
        mEdtCompanyAddr = (YunShlEditText) findViewById(R.id.edt_company_addr);
        mEdtCompanyInfo = (YunShlEditText) findViewById(R.id.edt_company_info);
        mBtnNext = (ThrottleButton) findViewById(R.id.btn_next);
        tv_warning_text = (TextView)findViewById(R.id.tv_warning_text);
    }

    @Override
    public void bindEvents() {

        mBtnNext.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CompanyBean companyBean = new CompanyBean();
                if (bean != null)
                    companyBean.setId(bean.getId());
                companyBean.setIntro(mEdtCompanyInfo.getTextString());
                companyBean.setCompany_location(mEdtCompanyAddr.getTextString());
                companyBean.setPos(mEdtCompanyPos.getTextString());
                if(userList != null && userList.size()>0){
                    LoadingDialog.Build(CompanyBaseInfoActivity.this).setContent("正在上传").show();
                    WPHSdk.getService(BusinessService.class).uploadPictures(userList, new YRequestCallback<List<BaseUrlBean>>() {
                        @Override
                        public void onSuccess(List<BaseUrlBean> var1) {
                            if (var1 != null && var1.size()>0){
                                companyBean.setLogo(var1.get(0).getUrl());
                                saveCompanyBase(companyBean);
                            }
                        }

                        @Override
                        public void onFailed(int var1, String message) {
                            LoadingDialog.dismissDialog();
                        }

                        @Override
                        public void onException(Throwable var1) {
                            LoadingDialog.dismissDialog();
                        }
                    });
                }else {
                    saveCompanyBase(companyBean);
                }




            }
        });

        mMViewBacktrack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mFlUploadImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoPickManager.selectPic(CompanyBaseInfoActivity.this, REQUEST_SELECT_PHOTO, 1, false, 0);
            }
        });
    }

    private void saveCompanyBase(CompanyBean companyBean) {
        WPHSdk.getService(UserInfoService.class)
                .saveCompanyBase(companyBean, new YRequestCallback<CompanyBean>() {
                    @Override
                    public void onSuccess(CompanyBean var1) {
                        Intent intent = new Intent(CompanyBaseInfoActivity.this, CompanyCaseCourseActivity.class);
                        if (bean != null){
                            intent.putExtra("bean",bean);
                        }
                        startActivity(intent);
                        LoadingDialog.dismissDialog();
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                        LoadingDialog.dismissDialog();
                    }

                    @Override
                    public void onException(Throwable var1) {
                        LoadingDialog.dismissDialog();
                    }
                });
    }

    @Override
    public void initData() {
        bean = getIntent().getParcelableExtra("bean");
        mFlUploadImg.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.getScreenWidth()*35/75));
        if (bean != null){
            if (bean.getAudit_status() == 21 ) {
                tv_warning_text.setVisibility(View.VISIBLE);
                tv_warning_text.setText(bean.getRemark());
            }
            mEdtCompanyInfo.setText(bean.getIntro());
            if (TextUtil.isNotEmpty(bean.getLogo())){
                mTvUploadImg.setVisibility(View.INVISIBLE);
                Glide.with(getApplicationContext()).load(bean.getLogo()).fitCenter().into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        mFlUploadImg.setBackground(resource);
                    }
                });
                userList = new ArrayList<>();
                List<String> strings = new ArrayList<>();
                strings.add(bean.getLogo());
                userList.addAll(UploadFileBean.create(strings));
            }
            mEdtCompanyAddr.setText(bean.getRegistered_address());
            mEdtCompanyPos.setText(bean.getPos());
            mTvCompanyName.setText(bean.getCompany_name());
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_PHOTO) {
            if (data != null) {
                Intent intent = new Intent(CompanyBaseInfoActivity.this, ClipActivity.class);
                intent.putExtra("data", PhotoPickManager.getPhoto(data));
                   intent.putExtra("scale",3);
                startActivityForResult(intent, CLIP_PHOTO);
            }
        } else if (requestCode == CLIP_PHOTO) {
            if (data != null) {
        //        LoadingDialog.Build(this).setContent("正在上传").show();
                ArrayList list = data.getParcelableArrayListExtra("result");
                userList = new ArrayList<>();
                userList.addAll(list);
                final String paths = userList.get(0).getPath();
                mTvUploadImg.setVisibility(View.INVISIBLE);
                Glide.with(getApplicationContext()).load(paths).fitCenter().into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        mFlUploadImg.setBackground(resource);
                    }
                });

            }
        }
    }
}
